import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_ui/services/auth_service.dart';
import 'package:flutter_ui/flavors/app_config.dart';

import '../main.dart';

/// 启动页面
///
/// 显示应用程序启动画面，停留3秒后根据登录状态自动跳转
/// 如果已登录，跳转到主页；否则跳转到登录页
class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage>
    with SingleTickerProviderStateMixin {
  // 动画控制器
  late AnimationController _animationController;
  late Animation<double> _opacityAnimation;

  // 认证服务
  final _authService = AuthService();

  @override
  void initState() {
    super.initState();

    // 初始化动画控制器
    _animationController = AnimationController(
      duration: const Duration(milliseconds: 1500),
      vsync: this,
    );

    // 透明度动画，从0.0到1.0
    _opacityAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(parent: _animationController, curve: Curves.easeIn),
    );

    // 启动动画
    _animationController.forward();

    // 初始化认证服务并设置延迟跳转
    _initAuthAndNavigate();
  }

  /// 初始化认证服务并设置延迟跳转 1
  Future<void> _initAuthAndNavigate() async {
    // 初始化认证服务
    await _authService.init();

    // 延迟3秒后跳转
    Timer(const Duration(seconds: 3), () {
      // 检查是否是EHR渠道
      if (AppConfig.instance.isEhr) {
        // EHR渠道，直接跳转到EHR WebView页面
        navigatorKey.currentState?.pushReplacementNamed('/ehr');
      } else {
        // 非EHR渠道，根据登录状态决定跳转目标
        if (_authService.isLoggedIn) {
          // 已登录，跳转到主页
          navigatorKey.currentState?.pushReplacementNamed('/home');
        } else {
          // 未登录，跳转到登录页
          navigatorKey.currentState?.pushReplacementNamed('/login');
        }
      }
    });
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(

          gradient: RadialGradient(
            colors: [Colors.white, Colors.white],
            center: Alignment.topCenter,
            radius: 2.0,
          ),
        ),
        child: FadeTransition(
          opacity: _opacityAnimation,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // Logo
              Container(
                width: 120,
                height: 120,
                decoration: BoxDecoration(
                  // color: Colors.white,
                  borderRadius: BorderRadius.circular(20),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.1),
                      blurRadius: 20,
                      spreadRadius: 5,
                    ),
                  ],
                ),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(20),
                  child: SvgPicture.asset(
                    'assets/images/app_icon.svg',
                    fit: BoxFit.cover,
                  ),
                ),
              ),
              const SizedBox(height: 30),
              // 应用名称
              const Text(
                '移动护理',
                style: TextStyle(
                  color: Colors.black87,
                  fontSize: 32,
                  fontWeight: FontWeight.bold,
                ),
              ),
              const SizedBox(height: 10),
              // 副标题
              const Text(
                '办公移动化',
                style: TextStyle(
                  color: Colors.black38,
                  fontSize: 18,
                  // 这里设置新的字体样式，例如斜体、粗体等，以下为示例
                  fontStyle: FontStyle.normal,
                  fontWeight: FontWeight.bold,
                ),
              ),
              const SizedBox(height: 50),
              // 加载指示器
              const CircularProgressIndicator(
                valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
              ),
              const SizedBox(height: 20),
              // 版权信息
              const Text(
                '© 重庆中软信息科技有限公司',
                style: TextStyle(color: Colors.black54, fontSize: 12),
              ),
            ],
          ),
        ),
      ),
    );
  }
}